package com.dailyblue.java.project.power.mapper;

import com.dailyblue.java.project.power.bean.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UsersMapper {
    @Select("select * from users where userName=#{userName} and password=md5(#{password})")
    Users login(@Param("userName") String userName, @Param("password") String password);

    @Select("select count(id) from users where userName=#{name}")
    int uniqueName(String name);

    @Insert("insert into users values(null,#{userName},md5(#{password}),#{nickName},null)")
    void saveUser(Users user);

    @Select("select count(id) from user_power where uid=#{uid} and pid =(select id from powers where url=#{url})")
    int checkUsersHasPermission(@Param("uid") int uid, @Param("url") String url);

    @Update("update users set roleId=#{rid} where id=#{uid}")
    void updateUserByRole(@Param("rid") int rid, @Param("uid") int uid);

    @Select("select * from users")
    List<Users> findAllUsers();

    // 修改用户的权限
    @Delete("delete from user_power where uid=#{uid}")
    void deleteUserPower(int uid);

    @Insert("insert into user_power values(null,#{uid},#{pid})")
    void saveUserPower(@Param("uid") int uid, @Param("pid") int pid);
}
